package com.video.controller.system;

import com.qf.entity.ResultData;
import com.video.commons.loginaop.LoginContact;
import com.video.commons.loginaop.LoginUser;
import com.video.commons.loginaop.MustLogin;
import com.video.commons.loginaop.NoMustLogin;
import com.video.commons.systemlog.LogType;
import com.video.commons.systemlog.SysLog;
import com.video.commons.utils.NetworkUtil;
import com.video.entity.User;
import com.video.entity.Webcon;
import com.video.netty.commons.utils.NettySSLUtil;
import com.video.service.system.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 后台系统登录管理Controller
 */
@Controller
@Slf4j
public class IndexController {

    @Value("${server.os}")
    private String os;

    @Autowired
    private NettySSLUtil nettySSLUtil;

    @Autowired
    private IUserService userService;

    /**
     * 进入系统登录页
     * @return
     */
    @NoMustLogin
    @RequestMapping("/system")
    public String welcome(){
        return "system/login";
    }

    /**
     * 跳转到系统的指定页面
     * @param page
     * @return
     */
    @RequestMapping("/topage/{page}")
    public String toPage(@PathVariable("page") String page){
        return "system/" + page;
    }


    @RequestMapping("/tostatuspage/{page}")
    public String toStatusPage(@PathVariable("page") String page){
        return page;
    }


    @RequestMapping("/toWebconPage/{page}")
    public String toWebconPage(@PathVariable("page") String page){
        return "webcon/" + page;
    }


    /**
     * 跳转到 直播流 视频页面
     * @param page
     * @return
     */
    @RequestMapping("/toVideoPage/{page}")
    @MustLogin(LoginContact.WEBCON_USER)
    public String toVideoPage(
            @PathVariable("page") String page,
            @LoginUser Webcon webcon,
            Model model){
        //获得websocket的连接url
        String wsPath = nettySSLUtil.getWsPath();
        //当前登录用户
        String account = webcon.getAccount();

        model.addAttribute("wsPath", wsPath);
        model.addAttribute("account", account);

        return "webcon/" + page;
    }


    /**
     * 登录操作
     * @param username
     * @param password
     * @param model
     * @return
     */
    @NoMustLogin
    @RequestMapping("/user/login")
    @SysLog(value = LogType.LOGIN, info = "进行后台系统登录操作")
    public String login(String username, String password, Model model, HttpSession session){

        User user = userService.login(username, password);
        if(user == null){
            model.addAttribute("error", 1);
            return "system/login";
        }

        session.setAttribute(LoginContact.BACK_USER.getKey(), user);
        return "system/index";
    }

    /**
     * 注销操作
     * @return
     */
    @NoMustLogin
    @RequestMapping("/logout")
    @SysLog(value = LogType.LOGOUT, info = "后台系统注销账户")
    public String logout(HttpSession session){
        session.removeAttribute(LoginContact.BACK_USER.getKey());
        return "system/login";
    }

    /**
     * 修改密码
     * @return
     */
    @SysLog(value = LogType.UPDATE, info = "修改管理员密码")
    @RequestMapping("/user/updatepassword")
    @MustLogin(LoginContact.BACK_USER)
    public String updatePassword(String newpassword, String oldpassword, Integer id, Model model){

        //修改密码
        int result = userService.updatePassword(newpassword, oldpassword, id);

        if(result == 0){
            model.addAttribute("error", 0);
            return "system/updatepassword";
        }

        return "mysucc";
    }

    @RequestMapping("/ping")
    @ResponseBody
    public ResultData<String> ping(String ip, String number){
        String str = NetworkUtil.ping(ip, number, os);
        return new ResultData<String>().setData(str);
    }
}
